package org.footito.android.data.update;

import java.util.ArrayList;
import java.util.GregorianCalendar;

import org.footito.android.api.CommentFromAPI;
import org.footito.android.api.GroupsTeamFromAPI;
import org.footito.android.api.RefreshParams;
import org.footito.android.api.StatusFromAPI;
import org.footito.android.api.TeamFromAPI;
import org.footito.android.connexion.FootitoConnexion;
import org.footito.android.data.database.FootitoDatabaseAdapter;
import org.footito.android.tools.Settings;
import org.footito.android.ui.activity.TimelineActivity;
import org.footito.android.ui.notifications.NotificationsManager;

import android.content.Context;
import android.os.AsyncTask;

public class UpdateDataTask extends AsyncTask<Void, Void, Void> {
	private FootitoConnexion connexion;
	private UpdateTaskListener listener;
	private Context context;
	private RefreshParams params;
	private boolean newStatuts = false;
	private int mStatusAdded;
	private NotificationsManager nm;

	public UpdateDataTask(Context context, UpdateTaskListener listener) {
		this.connexion = FootitoConnexion.getInstance(context);
		this.listener = listener;
		this.context = context;
		nm = new NotificationsManager(context);
	}

	public void setParams(RefreshParams params) {
		this.params = params;
	}

	private void performCompleteUpdate(RefreshParams params) {
		try {
			ArrayList<GroupsTeamFromAPI> groups = UpdateTools.performUpdateGroupsTeam(params,
					connexion, listener);
			ArrayList<TeamFromAPI> teams = UpdateTools.performUpdateTeams(params, connexion,
					listener);
			ArrayList<StatusFromAPI> status = UpdateTools.performUpdateStatus(params, connexion,
					listener);
			ArrayList<CommentFromAPI> comments = UpdateTools.performUpdateComments(params,
					connexion, listener);

			FootitoDatabaseAdapter dbAdapter = FootitoDatabaseAdapter.getInstance(context);
			dbAdapter.insertComments(comments);
			dbAdapter.insertGroupsTeam(groups);
			int statusAdded = dbAdapter.insertStatus(status);
			dbAdapter.insertTeams(teams);

			Settings.setLastCheck(context, new GregorianCalendar());
			Settings.setInitialized(context, true);

			if ((statusAdded > 0) && !TimelineActivity.isRunning) {
				Settings.incrementTimelinePositionOf(context, statusAdded);
				newStatuts = true;
				mStatusAdded = statusAdded;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	interface UpdateTaskListenerLocal extends UpdateTaskListener {
	}

	@Override
	protected void onPreExecute() {
		nm.showNotificationDownloading();
		super.onPreExecute();
	}

	@Override
	protected Void doInBackground(Void... params) {
		performCompleteUpdate(UpdateDataTask.this.params);
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		listener.onComplete(mStatusAdded);
		if (newStatuts) {
			nm.sendNewStatus(context, mStatusAdded);
		}
		nm.removeNotificationDownloading();
		super.onPostExecute(result);
	}

}
